/*
 * MovieNode.java
 * Describe SWF
 *
 * A browser for Flash files.
 * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * For more information please contact:
 * 
 * Stuart MacKay
 * Flagstone Software Ltd
 * 92 High Street
 * Wick, Caithness KW1 4LY
 * Scotland
 * 
 * www.flagstonesoftware.com
 */
package com.flagstone.describe;

import java.io.PrintWriter;
import java.util.Enumeration;

import javax.swing.tree.*;

final class MovieNode extends DefaultMutableTreeNode
{
    private static final long serialVersionUID = 0;
    private static String newline = System.getProperty("line.separator");
    
    public static void describeNode(MovieNode node, StringBuffer buffer, int level)
    {
        for (int i=0; i<level; i++)
            buffer.append('\t');
        
        if (node.getIndex() != -1)
            buffer.append("[" + node.getIndex() + "]");
        else
            buffer.append(node.displayName());      
        
        if (node.getIndex() != -1 && level != 0) 
        {
            if (node.getUserObject().toString().equals("Shape"))
            {
                buffer.append(" = Shape...").append(newline);
                return;
            }
            if (node.getUserObject().toString().equals("Bounds"))
            {
                buffer.append(" = Bounds...").append(newline);
                return;
            }
        }
        
        if (node.getChildCount() > 0)
        {
            buffer.append(" = ");
            buffer.append(node.getUserObject().toString());
            buffer.append(": {");
            buffer.append(newline);
            
            for (Enumeration e = node.children() ; e.hasMoreElements() ;) 
                describeNode((MovieNode)e.nextElement(), buffer, level+1);
    
            for (int i=0; i<level; i++)
                buffer.append('\t');
    
            buffer.append("};").append(newline);
        }
        else
        {
            buffer.append(" = ");
            
            if (node.isLeaf() == false)
            {
                if (node.isArray())
                    buffer.append("{}");
            }
            else
            {
                if (node.getUserObject() instanceof String)
                    buffer.append("\"" + node.getUserObject() + "\"");
                else
                    buffer.append(node.getUserObject().toString());
            }
            buffer.append(";").append(newline);
        }
    }
    public static void describeNode(MovieNode node, PrintWriter pw, int level)
    {
        for (int i=0; i<level; i++)
            pw.print('\t');
        
        if (node.getIndex() != -1)
            pw.print("[" + node.getIndex() + "]");
        else
            pw.print(node.displayName());
        
        if (node.getChildCount() > 0)
        {
            pw.print(" = ");
            pw.print(node.getUserObject().toString());
            pw.println(": {");
            
            for (Enumeration e = node.children() ; e.hasMoreElements() ;) 
                describeNode((MovieNode)e.nextElement(), pw, level+1);
    
            for (int i=0; i<level; i++)
                pw.print('\t');
    
            pw.println("};");
        }
        else
        {
            pw.print(" = ");
            
            if (node.isLeaf() == false)
            {
                if (node.isArray())
                    pw.print("{}");
            }
            else
            {
                if (node.getUserObject() instanceof String)
                    pw.print("\"" + node.getUserObject() + "\"");
                else
                    pw.print(node.getUserObject().toString());
            }
            pw.println(";");
        }
    }

    private String name;
    private int index;
    private int location;
    private int length;
    private boolean isArray;

    MovieNode(MovieNode parent, boolean isArray, int index, String name, int location, int length, Object value)
    {
        super(parent);
               
        this.name = name;
        this.isArray = isArray;
        this.location = location;
        this.length = length;
        this.index = index;

        setUserObject(value);
    } 
    boolean isArray()
    {
        return isArray;
    }
    int getIndex()
    {
        return index;
    }    
    void setIndex(int aValue)
    {
        index = aValue;
    }
    int getLocation()
    {
        return location;
    }    
    void setLocation(int aValue)
    {
        location = aValue;
    }
    int getLength()
    {
        return length;
    }    
    void setLength(int aValue)
    {
        length = aValue;
    }
    String getName()
    {
        return (name != null) ? name : "";
    }
    void setName(String aString)
    {
        name = aString;
    }

    String displayName()
    {
        String str;
        
        if (index != -1)
            str = "[" + index + "] : " + getUserObject().toString();
        else
            str = name;
            
        return str;
    }
    public String toString()
    {
        return displayName();
    }
}
